package com.wyy.travels.service.impl;/*
 **
 * @author yangyang
 *@date 2022/07/23 23:38
 **/

import com.wyy.travels.dao.PlaceDao;
import com.wyy.travels.entity.Place;
import com.wyy.travels.entity.Province;
import com.wyy.travels.service.PlaceService;
import com.wyy.travels.service.ProvinceDaoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
@Slf4j
public class PlaceServiceImpl implements PlaceService {
    @Autowired
    private PlaceDao placeDao;
    @Autowired
    private ProvinceDaoService provinceDaoService;
    @Override
    public List<Place> findByProvinceIdPage(Integer page, Integer rows, String provinceId) {
        int start=(page-1)*rows;
        List<Place> byProvinceIdPage = placeDao.findByProvinceIdPage(start, rows, provinceId);
        return byProvinceIdPage;
    }

    @Override
    public Integer findByProvinceIdCounts(String provinceId) {
        Integer byProvinceIdCounts = placeDao.findByProvinceIdCounts(provinceId);
        return byProvinceIdCounts;
    }

    @Override
    public void delete(String id) {
        Place place = placeDao.findOne(id);
        Province province = provinceDaoService.findOne(place.getProvinceid());
        province.setPlacecounts(province.getPlacecounts()-1);
        provinceDaoService.update(province);
        placeDao.delete(id);
    }

    @Override
    public void save(Place place) {
        placeDao.save(place);
        Province province = provinceDaoService.findOne(place.getProvinceid());
        province.setPlacecounts(province.getPlacecounts()+1);
        provinceDaoService.update(province);
    }

    @Override
    public Place findOne(String id) {
        Place one =placeDao.findOne(id);
        return one;
    }

    @Override
    public void update(Place place) {
        placeDao.update(place);
    }
}
